﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Nuclex.UserInterface;

namespace BrackIT
{
    public class Bracket : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private List<String> participants;
        private List<Nuclex.UserInterface.Controls.LabelControl> labels;
        private List<BracketItem> bracketItems;

        private Screen screen;

        private SpriteBatch spriteBatch;
        private Texture2D dark;
        private Texture2D light;

        public Bracket(Game1 game, Screen screen, List<String> participants)
            : base(game)
        {
            this.screen = screen;
            this.participants = participants;
            labels = new List<Nuclex.UserInterface.Controls.LabelControl>();
            bracketItems = new List<BracketItem>();
            Shuffle(participants);

            for (int i = 0; i < participants.Count; i++)
            {
                Nuclex.UserInterface.Controls.LabelControl lC = new Nuclex.UserInterface.Controls.LabelControl();
                lC.Text = participants[i];
                lC.Bounds = new UniRectangle(screen.Width - 225, 40.0f * (i + 1), 150.0f, 30.0f);

                labels.Add(lC);
            }
        }

        public void Shuffle<T>(IList<T> list)
        {
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        public void updateParticipants(List<String> newParticipants)
        {
            participants = newParticipants;
            Shuffle(participants);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            dark = new Texture2D(GraphicsDevice, 1, 1);
            dark.SetData(new Color[] {Color.Black});
            light = new Texture2D(GraphicsDevice, 1, 1);
            light.SetData(new Color[] {Color.Gray});

            base.LoadContent();
        }

        public override void Initialize()
        {
            foreach (Nuclex.UserInterface.Controls.LabelControl lC in labels)
            {
                screen.Desktop.Children.Add(lC);
            }

            base.Initialize();
        }

        //public void updateParticipants(List<String> newParticipants)
        //{
        //    this.participants = newParticipants;
        //}

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                //this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < participants.Count; i++)
            {
                labels[i].Text = participants[i];
            }

            base.Draw(gameTime);
        }
    }

    class BracketItem
    {
        private Rectangle background;
        private String participant;
        private Texture2D bgColor;

        public BracketItem(String participant, Texture2D bgColor)
        {
            this.participant = participant;
            this.bgColor = bgColor;
        }

        public void Draw()
        {
            
        }
    }
}
